# -*- coding: utf-8 -*-

from django.shortcuts import render, get_object_or_404, redirect

from myblog.models import Article
from .models import Comment
from .forms import CommentForm


def article_comment(request, pk):
    article = get_object_or_404(Article, pk=pk)

    if request.method == 'POST':
        form = CommentForm(request.POST)
        if form.is_valid():
            comment = form.save(commit=False)
            comment.article = article
            comment.save()
            return redirect(article)
        else:
            comment_list = article.comment_set.all()
            # comment_list = Comment.objects.filter(article=article_id)
            context = {
                'article': article,
                'form': form,
                'comment_list': comment_list
            }
            return render(request, 'blog/detail.html', context=context)

    return redirect(article)
